package com.cgly.medical.service.impl;

import com.cgly.medical.domain.Material;
import com.cgly.medical.mapper.MaterialMapper;
import com.cgly.medical.model.CityModel;
import com.cgly.medical.model.MaterialModel;
import com.cgly.medical.service.MaterialService;
import com.cgly.medical.util.Msg;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class MaterialServiceImpl implements MaterialService {

    @Resource
    MaterialMapper materialMapper;

    //查询所有必备材料数据
    @Override
    public Msg getAllMaterial(Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        List<Material> materials = materialMapper.getAllMaterial();
        PageInfo<Material> materialPageInfo=new PageInfo<>(materials,5);
        return Msg.success().data("materialList", materialPageInfo);
    }


    //根据条件查询必备材料信息：模糊查询
    @Override
    public Msg selectSomeMaterial(Material material) {
        //分页工具
        PageHelper.startPage(material.getPn(),material.getSize());
        //先根据条件查询到的material
        List<Material> listMaterial = materialMapper.selectSomeMaterial(material);

        if (listMaterial.size() == 0) {
            return Msg.fail().mess("未找到对应的城市信息");
        } else {
            PageInfo<Material> materialPageInfo=new PageInfo<>(listMaterial,5);
            return Msg.success().data("materialList", materialPageInfo);
        }

    }

    //根据id查找必备材料
    @Override
    public Msg selectMaterialById(Integer id) {
        Material material = materialMapper.selectMaterialById(id);
        if(material==null){
            return Msg.fail().mess("材料查询过错出错");
        }else{
            return Msg.success().data("material",material);
        }
    }


    //插入一条必备材料
    @Override
    public Msg insertMaterial(Material material) {
        Date date=new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(date);
        material.setCreateTime(format);
        material.setUpdateTime(format);

        int row = materialMapper.insertMaterial(material);
        if(row==0){
            return Msg.fail().mess("材料添加失败");
        }else{
            return Msg.success().data("row",row);
        }
    }


    //修改一条必备材料
    @Override
    public Msg updateMaterial(Material material) {

        Date date=new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(date);
        material.setUpdateTime(format);

        int row = materialMapper.updateMaterial(material);
        if(row==0){
            return Msg.fail().mess("材料更新失败");
        }else{
            return Msg.success().data("row",row);
        }
    }


    //    根据id删除必备材料的信息
    @Override
    public Msg deleteMaterialById(Integer id) {
        int row = materialMapper.deleteMaterialById(id);
        if(row==0){
            return Msg.fail().mess("必备材料删除失败");
        }else {
            return Msg.success().mess("必备材料删除成功");
        }
    }
}
